//-----------------------------------------------------------------------
//<copyright file="PhieuNhapBLL.cs
//Code gen by tool.
//</copyright>
//-----------------------------------------------------------------------
using System;
using PMS.Exceptions;
using System.Collections.Generic;
using PMS.DAL;
using PMS.Entity;
using System.Transactions;

namespace PMS.BLL
{
	public class PhieuNhapBO : BaseBO
	{
		// Interface Instant Object
		private readonly IPhieuNhapDAO phieunhapInterfaceObject = null;
        private readonly ChiTietPhieuNhapBO ChiTietPhieuNhapInterfaceObject = null;
        private readonly LuuKhoBO LuuKhoService = null;

		// Constructor
		public PhieuNhapBO()
		{
			phieunhapInterfaceObject = new DAL.ServiceFactory<IPhieuNhapDAO>().CurrentDAL;
            ChiTietPhieuNhapInterfaceObject = new ChiTietPhieuNhapBO();
            LuuKhoService = new LuuKhoBO();
		}

		#region PhieuNhap

		/// <summary>
		///
		/// </summary>
		/// <param name="phieunhap"></param>
		public PhieuNhap CreatePhieuNhap(PhieuNhap phieunhap,ChiTietPhieuNhaps DetailList)
		{
            string newstring = phieunhap.SoPN;
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    phieunhap = phieunhapInterfaceObject.CreatePhieuNhap(phieunhap);
                    foreach(ChiTietPhieuNhap item in DetailList)
                    {
                        item.SoPN = phieunhap.SoPN;

                        if (item.MaLuuKho == 0)
                        {
                            LuuKho luukho = item as LuuKho;
                            luukho.TongNhap = item.SoLuong;
                            item.TonKho = item.SoLuong;
                            item.MaLuuKho = LuuKhoService.CreateLuuKho(luukho).MaLuuKho;
                        }
                        else
                        {
                            LuuKhoService.UpdateLuuKhoNhapKho(item.MaLuuKho, item.SoLuong);
                        }
                        ChiTietPhieuNhapInterfaceObject.CreateChiTietPhieuNhap(item);
                    }
                }
                catch (Exception ex)
                {
                    phieunhap.SoPN = newstring;
                    throw BusinessException.GetException("Business Access CreatePhieuNhap Exception", ex);
                }
                scope.Complete();
            }
            return phieunhap;
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="phieunhap"></param>
        public void UpdatePhieuNhap(PhieuNhap phieunhap, ChiTietPhieuNhaps DetailList)
		{

            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    phieunhapInterfaceObject.UpdatePhieuNhap(phieunhap);
                    foreach (ChiTietPhieuNhap item in DetailList)
                    {
                        ChiTietPhieuNhap nhapCu = ChiTietPhieuNhapInterfaceObject.GetChiTietPhieuNhapByID(item.MaCTPN);
                        ChiTietPhieuNhapInterfaceObject.UpdateChiTietPhieuNhap(item);
                        LuuKho luuKho = LuuKhoService.GetLuuKhoByID(item.MaLuuKho);
                        luuKho.TonKho = luuKho.TonKho + (item.SoLuong - nhapCu.SoLuong);
                        luuKho.TongNhap = luuKho.TongNhap + (item.SoLuong - nhapCu.SoLuong);
                        LuuKhoService.UpdateLuuKho(luuKho);
                    }
                }
                catch (Exception ex)
                {
                    throw BusinessException.GetException("Business Access UpdatePhieuNhapException", ex);
                }
                scope.Complete();
            }
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="phieunhap"></param>
		public void DeletePhieuNhap(PhieuNhap phieunhap)
		{

            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    ChiTietPhieuNhapInterfaceObject.DeleteChiTietPhieuNhapBySoPN(phieunhap.SoPN);
                    phieunhapInterfaceObject.DeletePhieuNhap(phieunhap);
                    
                }
                catch (Exception ex)
                {
                    throw BusinessException.GetException("Business Access DeletePhieuNhapException", ex);
                }
                scope.Complete();
            }
			
		}

		/// <summary>
		///
		/// </summary>
		public PhieuNhaps GetAllPhieuNhap()
		{
			try
			{
				return phieunhapInterfaceObject.GetAllPhieuNhap();
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access GetAllPhieuNhap Exception", ex);
			}
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="soPN"></param>
		public PhieuNhap GetPhieuNhapByID(string soPN)
		{
			try
			{
				return phieunhapInterfaceObject.GetPhieuNhapByID(soPN);
			}
			catch (Exception ex)
			{
				throw BusinessException.GetException("Business Access GetPhieuNhapBy ID Exception", ex);
			}
		}

        public PhieuNhaps GetPhieuNhapByDate(DateTime? tungay, DateTime? denngay)
        {
            try
            {
                return phieunhapInterfaceObject.GetPhieuNhapByDate(tungay, denngay);
            }
            catch (Exception ex)
            {
                throw BusinessException.GetException("Business Access GetPhieuNhapByDate Exception", ex);
            }
        }

		#endregion
	}
}
